<?php 
namespace Common\Model;

use Think\Model;
class TagModel extends Model{
    protected $tableName = 'mall_tag';
    
    /**
     * 获取分组信息
     * @return unknown
     */
    public function getTag(){
        $sql = "SELECT mall_tag.id, mall_tag.`name`, mall_tag.create_time,COUNT(mall_goods.id) AS goods_num FROM mall_tag
                LEFT JOIN mall_goods_tag ON mall_goods_tag.tag_id = mall_tag.id
                LEFT JOIN mall_goods ON mall_goods.id=mall_goods_tag.goods_id AND mall_goods.is_del=0
                GROUP BY mall_tag.id";
        $rows = $this->query($sql);
        
        return $rows;
    }
    
    /**
     * 删除分组
     * @return unknown
     */
    public function delTage($id){
        if($id == ""){
            $this->error = '商品不存在';
            return -1;
        }
        
        $sql = "DELETE FROM mall_tag WHERE id IN (".$id.");";
        $this->execute($sql);
        
        $sql_goods = "DELETE FROM mall_goods_tag WHERE tag_id IN (".$id.");";
        $this->execute($sql_goods);
        
        return 1;
    }
}